package com.jxud.Pojo;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Product {
    private Integer id;
    @NotBlank(message = "商品名称不能为空")
    private String name;
    @NotBlank(message = "商品编码不能为空")
    private String code;
    @JsonProperty("brand_name")
    private String brandName;

    private BigDecimal price;
    @NotBlank(message = "ERPSKUID不能为空")
    @JsonProperty("erp_skuid")
    private String erpSkuid;
    @NotNull(message = "是否为组合商品不能为空")
    @JsonProperty("is_combination")
    private Integer isCombination; // 对应 TINYINT(1)，Java 中用 Boolean

    private String image;

    private Integer inventory;

    private String erpUrl;

    private String lv1Quantifier;

    private String barcode;

    private String classificationName;

    private Integer expiryDate;

    private String origin;

    private BigDecimal guidePrice;

    private BigDecimal procurementPricePurchaseWithTax;

    private BigDecimal referenceCost;

    private String skuProductTypeStr;
    private String supplierName;
    private String classify;

    private String taste;
    @JsonProperty("quality_report_url")
    private String qualityReportUrl;
    @JsonProperty("material_url")
    private String materialUrl;
    @JsonProperty("package_contents")
    private String packageContents;
    @JsonProperty("selling_points")
    private String sellingPoints;

    @JsonProperty("tags")
    private List<String> tags;    // Java 使用的标签列表
    private String tagsJson;     // 数据库存储的 tags JSON

    @JsonProperty("faq_json")
    private List<FaqItem> faqs;      // 可以是 JSON 字符串，或映射为 Map<String, String>
    private String faqJson;            // 数据库存储的 faq JSON

    private String projectName;      //开票项目名称
    private Integer taxRate;      //税率

    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public List<String> getTags() {
        if (tags == null) {
            return new ArrayList<>(); // 返回空列表而不是 null
        }
        return new ArrayList<>(tags);
    }

    public List<FaqItem> getFaqs() {
        if (faqs == null) {
            return new ArrayList<>();
        }
        return new ArrayList<>(faqs);
    }

    public String getTagsJson() {
        return tagsJson;
    }

    public String getFaqJson() {
        return faqJson;
    }


//     Setter：更新 tags 时同步 JSON
    public void setTags(List<String> tags) {
        this.tags = new ArrayList<>(tags);
        try {
            this.tagsJson = objectMapper.writeValueAsString(this.tags);
        } catch (Exception e) {
            throw new RuntimeException("Failed to serialize tags to JSON", e);
        }
    }

    // Setter：更新 faqs 时同步 JSON
    public void setFaqs(List<FaqItem> faqs) {
        this.faqs = new ArrayList<>(faqs);
        try {
            this.faqJson = objectMapper.writeValueAsString(this.faqs);
        } catch (Exception e) {
            throw new RuntimeException("Failed to serialize faqs to JSON", e);
        }
    }


}
